﻿
Imports System.IO
Imports osi.root.constants
Imports osi.root.delegates
Imports osi.root.formation
Imports osi.root.connector
Imports osi.root.procedure
Imports osi.root.utils
Imports osi.service.device
Imports osi.service.convertor
Imports Encoding = System.Text.Encoding

Public Class text_herald_adapter_convertor
    Public Shared ReadOnly string_command As binder(Of _do_val_ref(Of String, command, Boolean), 
                                                       uri_conversion_binder_protector)
    Public Shared ReadOnly command_string As binder(Of _do_val_ref(Of command, String, Boolean), 
                                                       uri_conversion_binder_protector)

    Public Shared Function convert(ByVal i As String, ByRef o As command) As Boolean
        ' FIXME: the status of command.from_uri cannot return to respond
        '        without breaking the procedure
        o = New command()
        If Not o.from_uri(i) Then
            o.clear()
            assert(o.empty())
        End If
        Return True
    End Function

    Shared Sub New()
        string_command = New binder(Of _do_val_ref(Of String, command, Boolean), uri_conversion_binder_protector)()
        string_command.set_local(AddressOf convert)
    End Sub

    Private Sub New()
    End Sub
End Class

Public Class text_herald_adapter
    Inherits text_dev_T_adapter(Of command)
    Implements herald

    Public Sub New(ByVal t As text)
        MyBase.New(t,
                   text_herald_adapter_convertor.command_string,
                   text_herald_adapter_convertor.string_command)
    End Sub

    Public Shared Shadows Function convert(ByVal i As text, ByRef o As herald) As Boolean
        o = New text_herald_adapter(i)
        Return True
    End Function

    Public Shared Shadows Function convert(ByVal i As text, ByVal o As pointer(Of herald)) As event_comb
        Return sync_async(Function() As Boolean
                              Dim r As herald = Nothing
                              Return convert(i, r) AndAlso
                                     eva(o, r)
                          End Function)
    End Function

    Public Shared Shadows Function convert(ByVal i As herald, ByRef o As text) As Boolean
        Return text_dev_T_adapter(Of command).convert(i, o)
    End Function
End Class

Public Class stream_text_herald_adapter
    Inherits stream_text_dev_T_adapter(Of command)
    Implements herald

    Public Sub New(ByVal t As stream_text,
                   Optional ByVal enc As Encoding = Nothing)
        MyBase.New(t,
                   enc,
                   text_herald_adapter_convertor.command_string,
                   text_herald_adapter_convertor.string_command)
    End Sub

    Public Shared Shadows Function convert(ByVal i As stream_text, ByRef o As herald) As Boolean
        o = New stream_text_herald_adapter(i)
        Return True
    End Function

    Public Shared Shadows Function convert(ByVal i As stream_text, ByVal o As pointer(Of herald)) As event_comb
        Return sync_async(Function() As Boolean
                              Dim r As herald = Nothing
                              Return convert(i, r) AndAlso
                                     eva(o, r)
                          End Function)
    End Function

    Public Shared Shadows Function convert(ByVal i As herald, ByRef o As stream_text) As Boolean
        Return stream_text_dev_T_adapter(Of command).convert(i, o)
    End Function
End Class
